public class demo4 {

    public static void main(String[] args) throws InterruptedException {
        //拿到当前 main 线程对象
        Thread mainThread = Thread.currentThread();

        Thread t1 = new Thread(()->{

            //在 t1 线程中调用 join() 方法，
            //阻塞当前 t1 线程，等待 main 线程执行完毕
            try {
                mainThread.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("正在执行 t1 线程");
            }

        });
        t1.start();

        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println("正在执行 main 线程");
        }

    }
}
